#include <QProcess>
#include <QSettings>
//#include <QMessageBox>

#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setFixedSize(this->size());
    ui->epoch->setText(QString::number(time(0)+180));
    ui->msaa->setValue(16);
    ui->asteroids->setValue(64);

    QSettings settings;
    settings.beginGroup(this->objectName());
    QVariant center = settings.value("center");
    if(!center.isNull())
    {
        ui->center->setCheckState(Qt::CheckState(center.toInt()));
        ui->tilt->setCheckState(Qt::CheckState(settings.value("tilt").toInt()));
        ui->msaa->setValue(settings.value("msaa").toInt());
        ui->asteroids->setValue(settings.value("asteroids").toInt());
        ui->hostname->setText(settings.value("hostname").toString());
    }
    settings.endGroup();
}

MainWindow::~MainWindow()
{
    QSettings settings;
    settings.beginGroup(this->objectName());
    settings.setValue("center", QString::number(ui->center->checkState()));
    settings.setValue("tilt", QString::number(ui->tilt->checkState()));
    settings.setValue("msaa", QString::number(ui->msaa->value()));
    settings.setValue("asteroids", QString::number(ui->asteroids->value()));
    settings.setValue("hostname", ui->hostname->text());
    settings.endGroup();
    delete ui;
}

void MainWindow::on_quit_clicked()
{
    QApplication::quit();
}

void MainWindow::on_join_clicked()
{
    QStringList argc;
    argc.append(ui->epoch->text());
    argc.append(ui->hostname->text());
    argc.append(QString::number(ui->asteroids->value()));
    argc.append(QString::number(ui->msaa->value()));
    argc.append(QString::number(ui->tilt->isChecked()));
    argc.append(QString::number(ui->center->isChecked()));

    QProcess proc;
    proc.setProgram("fractalattackonline");
    proc.setArguments(argc);
    proc.startDetached();

//    QMessageBox msgBox;
//    msgBox.setText("fractalattackonline " + ui->epoch->text() + " " + ui->hostname->text() + " " + QString::number(ui->asteroids->value()) + " " + QString::number(ui->msaa->value()) + " " + QString::number(ui->tilt->isChecked()) + " " + QString::number(ui->center->isChecked()));
//    msgBox.exec();
}


void MainWindow::on_reset_clicked()
{
    ui->epoch->setText(QString::number(time(0)+180));
    ui->msaa->setValue(16);
    ui->asteroids->setValue(64);
    ui->center->setCheckState(Qt::CheckState(2));
    ui->tilt->setCheckState(Qt::CheckState(2));
    ui->hostname->setText("vfcash.co.uk");
}

